package pagetest;/**
 * Create by IntelliJ IDEA
 *
 * @author: MaoYongQi
 * @date-Time: 2018/4/23 10:56
 * @description:
 */

import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.Response;

import java.util.*;

/**
 * Create by IntelliJ IDEA
 * @author: MaoYongQi
 * @date-Time: 2018/4/23 10:56
 * @description:
 */
public class RedisUtil {
    //缓存时间
    public static final Integer SECONDS=3*60*60*24;

    /**
     * 查询缓存中的数据
     * @param jedis
     * @param keyIds        id集合的key值
     * @param objectKeyId   对象key值的SYS:BILL:前缀  如："SYS:BILL:"+订单号
     * @param num 当前页数
     * @param pageCount  每页条数
     * @return
     */
    public static  List<Map<String, String>> findPagedQuery(String keyIds, String objectKeyId, Long num, Long pageCount,Jedis jedis) {
        List<Map<String, String>> list = new ArrayList<Map<String, String>>();
        //获取管道对象
        Pipeline pipeline = jedis.pipelined();
        // 开始记录数
        Long startCount = (num - 1) * pageCount;
        List<String> lrange = jedis.lrange(keyIds, startCount, startCount + pageCount - 1);
        if (startCount.intValue() != 0 && lrange.size() != pageCount) {
            // 当缓存中数据不够时直接返回空
            return null;
        }
        // 使用pipeline 批量获取数据
        Map<String, Response<Map<String, String>>> responses = new HashMap<String, Response<Map<String, String>>>();
        for (String key : lrange) {
            String objectKey = objectKeyId + key;
            responses.put(key, pipeline.hgetAll(objectKey));
        }
        // 同步数据
        pipeline.sync();
        for (String key : responses.keySet()) {
            Map<String, String> map = new HashMap<String, String>();
            // 获取订单对象
            map = responses.get(key).get();
            list.add(map);
        }
        return list;
    }




    /**
     * 存入对象数据
     * @param list
     * @param keyIds       redis ids集合的 的key名称
     * @param objectKeyId  redis 对象的 的key名称
     * @param majorKey     list 的主键名称
     */
    public static void setCacheObjectInfo(List<Map<String, Object>> list,String keyIds,String objectKeyId,String majorKey, Jedis jedis) {
        //获取管道对象
        Pipeline pipeline = jedis.pipelined();
        jedis.del(keyIds);
        for (Map<String, Object> object : list) {
            //存储 对象 信息
            pipeline.rpush(keyIds, object.get(majorKey).toString());
            //获取封装好的对象数据
            Map<String, String> billMap = getEncapsulationObject(object);
            String key=objectKeyId+object.get(majorKey);
            pipeline.hmset(key, billMap);
            pipeline.expire(key, SECONDS);
        }
        pipeline.expire(keyIds, SECONDS);
        pipeline.sync();
    }


    /**
     * 将数据转换成字符串
     * @param object
     * @return
     */
    public static Map<String, String> getEncapsulationObject(Map<String, Object> object){
        Map<String, String> billMap = new HashMap<String, String>();
        for(String key:object.keySet()){
            billMap.put(key,convertString(object.get(key)));
        }
        return billMap;
    }



    /**
     * 转换对象
     * 当对象是一个时间类型 转成时间戳
     * 其他类型都为字符串
     * @param object
     * @return
     */
    public static String convertString(Object object){
        Object obj=object;
        if(obj == null){
            return "";
        } else if(obj instanceof Date){
            obj = ((Date)obj).getTime();
            return obj.toString();
        }
        if(isBlank(obj.toString())){
            return "";
        }
        return obj.toString();
    }


    /**
     * 判断字符串是否为空
     * @param str
     * @return
     */
    public static boolean isBlank(String str)
    {
        if( str == null || str.length() == 0 )
            return true;
        return false;
    }
}
